<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="handleClose" @open="handleOpen">
    <!-- 表单 -->
    <el-form label-width="120px">
      <el-form-item label="姓名" prop="username" :model="formData">
        <el-input v-model="formData.username" style="width:80%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" style="width:80%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="formData.timeOfEntry" style="width:80%" placeholder="请选择入职时间" value-format="yyyy-MM-dd" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="">
        <el-select v-model="formData.formOfEmployment" style="width:80%" placeholder="请选择">
          <el-option
            v-for="item in hireTypes"
            :key="item.id"
            :value="item.id"
            :label="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" style="width:80%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="formData.departmentName" readonly style="width:80%" placeholder="请选择部门" @focus="isShow = true" />
        <!-- 部门树形组件 -->
        <el-tree v-show="isShow" :data="deptTreeData" :props="{ label: 'name'}" @node-click="handleNodeClick" />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="formData.correctionTime" style="width:80%" placeholder="请选择转正时间" value-format="yyyy-MM-dd" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="handleAdd">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
// 导入常量模块
import obj from '@/constant/employees'

import { getDeptListAPI } from '@/api/departments'

import { list2Tree } from '@/utils/shift'
import { addEmployeeAPI } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 员工表单
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },

      hireTypes: obj.hireType,
      // 部门树形数据
      deptTreeData: [],
      // 控制树形组件的显示和隐藏
      isShow: false
    }
  },
  methods: {
    // 弹框关闭时
    handleClose() {
      this.$emit('update:showDialog', false)
    },
    // 弹框打开时
    async handleOpen() {
      // 发起获取部门列表请求
      const resp = await getDeptListAPI()
      // console.log(resp)
      // 转为树形结构再赋值
      this.deptTreeData = list2Tree(resp.data.depts, '')
    },
    // 树形节点点击时
    handleNodeClick(node) {
      // console.log(node)
      // node：被点击的部门对象
      // 如果有子部门，直接结束
      if (node.chaildren) return
      // 否则赋值当前选择的部门
      this.formData.departmentName = node.name
      // 隐藏树形组件
      this.isShow = false
    },
    // 新增员工
    async handleAdd() {
      // 整体校验
      // 发起新增请求
      await addEmployeeAPI(this.formData)
      // 关闭弹框
      this.handleClose()
      // 给个成功的提示
      this.$message.success('新增成功')
      // 获取员工列表
      // this.$parent.getEmployeeList()
      // 推荐子传父
      this.$emit('add-success')
    }
  }
}
</script>

  <style lang="scss" scoped>
  .tree-box {
  position: absolute;
  width: 50%;
  min-height: 50px;
  left: 0;
  top: 45px;
  z-index: 100;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding-right: 5px;
  overflow: hidden;
  background-color: #fff;
}
  </style>
